import * as vscode from 'vscode';
import * as path from 'path';

export function activate(context: vscode.ExtensionContext) {
    let disposable = vscode.workspace.onDidOpenTextDocument((document) => {
        if (document.uri.fsPath.toLowerCase().endsWith('.mp4')) {
            openVideoPlayer(document.uri);
        }
    });

    // 注册自定义编辑器提供程序
    context.subscriptions.push(
        vscode.window.registerCustomEditorProvider('mp4vsplayer.preview', {
            async resolveCustomEditor(
                document: vscode.CustomDocument,
                webviewPanel: vscode.WebviewPanel,
                _token: vscode.CancellationToken
            ): Promise<void> {
                const uri = document.uri;
                const videoPath = webviewPanel.webview.asWebviewUri(uri);

                webviewPanel.webview.options = {
                    enableScripts: true
                };

                webviewPanel.webview.html = `
                    <!DOCTYPE html>
                    <html>
                        <head>
                            <style>
                                body { 
                                    margin: 0;
                                    padding: 0;
                                    height: 100vh;
                                    display: flex;
                                    justify-content: center;
                                    align-items: center;
                                    background: #000;
                                }
                                video {
                                    max-width: 100%;
                                    max-height: 100vh;
                                }
                            </style>
                        </head>
                        <body>
                            <video controls autoplay>
                                <source src="${videoPath}" type="video/mp4">
                            </video>
                        </body>
                    </html>
                `;
            },
            // 添加必需的方法
            async openCustomDocument(
                uri: vscode.Uri,
                openContext: vscode.CustomDocumentOpenContext,
                token: vscode.CancellationToken
            ): Promise<vscode.CustomDocument> {
                return { uri, dispose: () => {} };
            },
            // 以下方法对于只读文档来说可以是空实现
            async saveCustomDocument(document: vscode.CustomDocument): Promise<void> {},
            async saveCustomDocumentAs(document: vscode.CustomDocument, destination: vscode.Uri): Promise<void> {},
            async revertCustomDocument(document: vscode.CustomDocument): Promise<void> {},
            async backupCustomDocument(document: vscode.CustomDocument, context: vscode.CustomDocumentBackupContext): Promise<vscode.CustomDocumentBackup> {
                return {
                    id: document.uri.toString(),
                    delete: () => {}
                };
            }
        })
    );

    context.subscriptions.push(disposable);
}

function openVideoPlayer(uri: vscode.Uri) {
    vscode.commands.executeCommand('vscode.openWith', uri, 'mp4vsplayer.preview');
}

export function deactivate() {}